package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckitemController {
    @Autowired
    private CheckitemService checkitemService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询，{}", queryPageBean);
        PageResult pageResult = checkitemService.findPageWithQueryString(queryPageBean);
        return pageResult;
    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项，{}", checkItem);
        try {
            checkitemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL, null);
        }
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.info("根据id删除检查项，{}", id);
        try {
            checkitemService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL, null);
        }
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询检查项，{}", id);
        try {
            CheckItem checkItem = checkitemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, null);
        }
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项，{}", checkItem);
        try {
            checkitemService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL, null);
        }
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        try {
            List<CheckItem> checkItems = checkitemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, null);
        }
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        log.info("根据检查组id查询关联的检查项id，{}", checkgroupId);
        try {
            List<Integer> checkItemIds = checkitemService.findCheckItemIdsByCheckGroupId(checkgroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, null);
        }
    }
}
